import { successResponse, errorResponse } from 'server/utils/response'
import { Projects } from 'server/models/Project'

export default defineEventHandler(async (event) => {
  try {
    const { id } = getRouterParams(event)

    if (!id) {
      return errorResponse('项目ID不能为空')
    }

    const projectId = parseInt(id)
    if (isNaN(projectId)) {
      return errorResponse('项目ID格式不正确')
    }

    // 获取项目详情
    const project = await Projects.getProjectById(projectId)

    if (!project) {
      return errorResponse('项目不存在')
    }

    // 前台只显示已发布的项目
    if (project.status !== 'published') {
      return errorResponse('项目不存在')
    }

    // 增加浏览量
    await Projects.incrementViewCount(projectId)

    return successResponse(project, '获取项目详情成功')
  } catch (error) {
    console.error('获取项目详情失败:', error)
    return errorResponse('获取项目详情失败', error instanceof Error ? error.message : '未知错误')
  }
})